<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ArticleController extends Controller
{
	public function getAdd(){
		$articlecates=DB::table('articlecate')->select(DB::raw('*, concat(path,",",id) as pd'))->orderBy('pd')->get();
        return view('admin.article.add',['cates'=>$articlecates]);
	}
	public function postInsert(Request $request){
		$this->validate($request,[
            'title'=>'required',
            'content'=>'required'
            ],[
            'title.required'=>'标题不能为空',
            'content.required'=>'文章内容不能为空'
            ]);
		$request['time']=time();
		$article=$request->except('_token');
		$res=DB::table('article')->insert($article);

		if($res){
            return redirect('article/list')->with('info','添加分类成功');
        }else{
            return back()->with('error','添加分类失败');
        }
	}
	public function getList(Request $request){

		$article=DB::table('article')
            ->orderBy('time','desc')
            ->where(function($query)use($request){
                $keyword=$request->input('keyword');
                if($keyword){
                    $query->where('title','like','%'.$keyword.'%');
                }
            })
            ->paginate($request->input('num',10));
        foreach($article as $k=>$value){
            $p = DB::table('articlecate')->where('id','=',$value->articlecate_id)->first();
            if($p){
                $path='';
                $arr=explode(",",$p->path);
                foreach($arr as $v){
                    // global $value;
                    $j=DB::table('articlecate')->select('name')->where('id','=',$v)->first();
                    if($j){
                        $path .= $j->name.'/';
                    }else{
                        $path .= '顶级分类/';
                    }
                }
                $path .= $p->name;
            }else{
                $path = '顶级分类/';
            }
            $value->path=$path;
            if($value->status == 1){
                $value->status ='正常';
            }else{
                $value->status ='冻结';
            }
        }
        return view('admin.article.list',['article'=>$article,'request'=>$request]);
	}

	public function getEdit(Request $request){
		$id=$request->input('id');
		$article=DB::table('article')->where('id','=',$id)->first();
		$cate=DB::table('articlecate')->where('id','=',$article->articlecate_id)->first();
		return view('admin.article.edit',['article'=>$article,'cate'=>$cate]);
	}
	public function postUpdate(Request $request){
        //获取id
        $id=$request->id;
        //获取更新的文章内容
        $newc=$request->content;
        //匹配要更新文章内图片
        $z1='/src="(.*?)"/';
        preg_match_all($z1, $newc, $newimg);
        if(isset($newimg[1])){
            //获取旧文章的图片
            $img=DB::table('article')->where('id',$id)->value('content');
            $z='/src="(.*?)"/';
            preg_match_all($z, $img, $nimg);
            if(isset($nimg[1])){
                foreach ($nimg[1] as $k => $v) {
                    if(!in_array($v, $newimg[1])){
                        @unlink('.'.$v);
                    }
                }
            }
        }
		$this->validate($request,[
            'title'=>'required',
            'content'=>'required'
            ],[
            'title.required'=>'标题不能为空',
            'content.required'=>'文章内容不能为空'
            ]);
		$request['time']=time();
		$article=$request->except('_token');
		$res=DB::table('article')->where('id',$id)->update($article);
		if($res){
            return redirect('article/list')->with('info','修改文章成功');
        }else{
            return back()->with('error','修改文章失败');
        }
	}
	public function getDrop(Request $request){
		$id=$request->input('id');
        $img=DB::table('article')->where('id','=',$id)->get();
        if($img){
            $img_path=$img[0]->content;
            $z='/src="(.*?)"/';
            preg_match_all($z, $img_path, $j);
            if($j){
                foreach ($j[1] as $k => $v) {
                    @unlink('.'.$v);
                }
            }
        }
		$res=DB::table('article')->where('id','=',$id)->delete();
		if($res){
            return redirect('article/list')->with('info','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
	}
}
